from settings import *


class Timer:
    def __init__(self, duration, func=None):
        self.duration = duration
        self.func = func
        self.start_time = 0
        self.active = False
        self.args = ()
        self.kwargs = {}

    def activate(self,  *args, **kwargs):
        self.active = True
        self.start_time = PG.time.get_ticks()
        self.args = args
        self.kwargs = kwargs

    def deactivate(self):
        self.active = False
        self.start_time = 0

    def update(self):
        current_time = PG.time.get_ticks()
        if self.active and current_time - self.start_time >= self.duration:
            if self.func and self.start_time != 0:
                self.func(*self.args, **self.kwargs)
            self.deactivate()
